# -*- coding: utf-8 -*-
"""
Created on Tue Apr  1 15:01:54 2025

@author: micha
"""


import pandas as pd
pd.options.plotting.backend = "plotly"
import plotly.io as pio
import plotly.graph_objects as go
import sys

sys.path.append("C:/Michael/Albany LAIO Lab and Research Projects/Python Utilities")

#send plots to current browser. Export from browser as .png
pio.renderers.default='browser'

'''
Flags
'''
autoscale=True # autoscale flag. If true use autoscale on figures
save_images=True # If true save images to disk.

Image_dir = "C:/Michael/Albany LAIO Lab and Research Projects/Is a profile worth/images/"

Data_dir = "C:/Michael/Albany LAIO Lab and Research Projects/Is a profile worth/data/"

   
Confidence_Intervals_DF=pd.read_excel("C:/Michael/Albany LAIO Lab and Research Projects/Is a profile worth/data/Confidence Intervals.xlsx")

    

for variable in ['BACE','CC','DIS','IGB','PWR','SC','TASK','nACH-100','nAFF-100','nPWR-100','I1','P1']:
    
    Variable_DF=Confidence_Intervals_DF[Confidence_Intervals_DF['Variable']==variable]
    
   
    fig = go.Figure()

    fig.update_xaxes(title='Words')
    fig.update_yaxes(title= 'Distance from Leader Mean')
    fig.update_layout(title='100, 95, and 84% Confidence Intervals for'+' '+variable+' by Number of Words')

    # Add traces
    fig.add_trace(go.Scatter(y=Variable_DF['Max_Distance'],
                        x=Variable_DF['Minimum'],
                        mode='lines',
                        line=dict(width=1,color='black'),
                        showlegend=False
                        ))
    
    fig.add_trace(go.Scatter(y=Variable_DF['Min_Distance'],
                        x=Variable_DF['Minimum'],
                        mode='lines',
                        line=dict(width=1,color='black'),
                        showlegend=False,
                        fillcolor='rgba(68, 68, 68, 0.3)',
                        fill='tonexty',
                        ))
    
    fig.add_trace(go.Scatter(y=Variable_DF['CI95_Upper_Bound'],
                        x=Variable_DF['Minimum'],
                        mode='lines',
                        line=dict(width=1,color='red'),
                        showlegend=False
                        ))
    
    fig.add_trace(go.Scatter(y=Variable_DF['CI95_Lower_Bound'],
                        x=Variable_DF['Minimum'],
                        mode='lines',
                        line=dict(width=1,color='red'),
                        showlegend=False,
                        fillcolor='rgba(68, 68, 68, 0.3)',
                        fill='tonexty',
                        ))
    
    fig.add_trace(go.Scatter(y=Variable_DF['CI84_Upper_Bound'],
                        x=Variable_DF['Minimum'],
                        mode='lines',
                        line=dict(width=1,color='aqua'),
                        showlegend=False
                        ))
    
    fig.add_trace(go.Scatter(y=Variable_DF['CI84_Lower_Bound'],
                        x=Variable_DF['Minimum'],
                        mode='lines',
                        line=dict(width=1,color='aqua'),
                        showlegend=False,
                        fillcolor='rgba(68, 68, 68, 0.5)',
                        fill='tonexty',
                        ))
   
    fig.update_traces(connectgaps=True)
    
    fig.show()
    
    if save_images:
        fig.write_image(Image_dir+"Confidence Intervals for "+variable+".svg", format='svg',scale=2, engine='orca')
        
    Variable_DF.to_excel(excel_writer=Data_dir+"Confidence Intervals  for "+variable+".xlsx", index=False, columns=['Variable','Minimum','Draws','Max_Distance','Min_Distance','CI95_Upper_Bound','CI95_Lower_Bound','CI84_Upper_Bound','CI84_Lower_Bound'])  